<?php
/**
 * @author cattaka.net
 */

require_once 'bootstrap.inc.php';

$params = array_merge($_GET,$_POST);
if (!isset($params["id"])) {
	die('パラメータ"id"が無いよー');
}
if (!isset($params["name"])) {
	die('パラメータ"name"が無いよー');
}
$id = $params["id"];
$name = $params["name"];

$dbh = NULL;
if ($dbh == NULL) {
	$dbh = new PDO($databaseDsn, NULL, NULL);
	$dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
} else {
	throw new Exception(MSG_DB_ALREADY_CONNECTED);
}

$member = null;
if ($stmt = $dbh->prepare("INSERT INTO MEMBERS(ID,NAME) VALUES (?,?)")) {
	try {
		if ($stmt->execute(array($id,$name))) {
			// OK
			$member = array("id" => $id, "name" => $name);
		} else {
			die("あれ？");
		}
	} catch (PDOException $e) {
		die("IDが重複してるぜ(多分)");
	}
} else {
	die("Execute sql failed.");
}

$memberList=array();
if ($stmt = $dbh->query("SELECT ID,NAME FROM MEMBERS")) {
	while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
		$memberList[] = array("id"=>$row["ID"], "name"=>$row["NAME"]);
	}
	$stmt->closeCursor();
} else {
	die("Execute sql failed.");
}
$dbh = NULL;

?>
